library(plotly);
library(ggplot2);
library(ggfortify);
library(repr);
library(tidyr);
library(tsibble);
library(TSstudio);
library(anytime);
library(zoo);
Loading required package: ggplot2


Attaching package: 'plotly'


The following object is masked from 'package:ggplot2':

    last_plot


The following object is masked from 'package:stats':

    filter


The following object is masked from 'package:graphics':

    layout



Attaching package: 'tsibble'


The following objects are masked from 'package:base':

    intersect, setdiff, union



Attaching package: 'zoo'


The following object is masked from 'package:tsibble':

    index


The following objects are masked from 'package:base':

    as.Date, as.Date.numeric


Using adjusted

dataset = read.csv("dataset/stockholm_monthly_mean_temperature_1980_2020_adjust.csv", sep=';')
#rownames(dt) = as.integer(as.character(dt$year))
min_year = min(dataset$year)
max_year = max(dataset$year)
#dt = dt[,-1]
head(dataset)
A data.frame: 6 × 13
yearjanfebmaraprmayjunjulaugsepoctnovdec
<int><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
11980-4.7 -6.2-3.14.8 7.815.616.814.612.85.70.0-0.8
21981-4.0 -2.7-2.23.910.812.616.414.611.76.51.1-5.7
31982-7.2 -4.0 1.14.1 9.412.517.417.012.37.64.5 0.3
41983 0.7 -3.8-0.14.010.214.018.017.812.47.51.2-1.1
51984-3.0 -1.7-2.05.310.913.315.316.410.68.94.3 0.8
61985-9.0-11.6-1.71.6 9.013.615.915.410.38.00.3-4.3
train = ts(as.vector(t(as.matrix(dataset[,-1]))), start=c(1980, 1), end=c(max_year, 12), frequency =12)
dt_reshape <- data.frame(date=as.Date(as.yearmon(time(train))), temp=as.matrix(train))

#dt_reshape <- dataset %>% pivot_longer(cols=jan:dec, names_to = "month", values_to = "temp") %>%
#            unite(date, c(year, month))
#dt_reshape$date <- anydate(dt_reshape$date)
#print(dt_reshape)
head(dt_reshape)
A data.frame: 6 × 2
datetemp
<date><dbl>
11980-01-01-4.7
21980-02-01-6.2
31980-03-01-3.1
41980-04-01 4.8
51980-05-01 7.8
61980-06-0115.6
options(repr.plot.width=15, repr.plot.height=8)
autoplot(train, xlab="Year", ylab="Temperature (°C)")+
    ggtitle("Monthly Mean Air Temperature (°C), Stockholm 1980-2020")+ 
    theme(plot.title = element_text(size = 35, face = "bold")) +
    guides(colour = guide_legend(title.hjust = 20))
fig <- plot_ly(dt_reshape, x = ~date, y = ~temp, type = 'scatter', mode = 'lines') %>%
        layout(title = 'Monthly Mean Air Temperature (°C)', plot_bgcolor = "#e5ecf6")
fig
<!doctype html>
boxplot(train~cycle(train), names=month.abb, xlab="", ylab="Temperature (°C)")
fig <- plot_ly(dt_reshape, y=~temp, type = "box")
fig
# https://stackoverflow.com/questions/40622933/rplotly-creating-multiple-boxplots-in-one-graph-as-a-group
<!doctype html>

For example, a root near 1 of the autoregressive polynomial suggests that the data should be differenced before fitting an ARMA model, whereas a root near 1 of the moving-average polynomial indicates that the data were overdifferenced.